﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;
using FilmBook.DataServices;
using System.Runtime.Serialization.Formatters.Binary;

namespace Dataloader
{
    public class Program
    {
        public static void Main(string[] args)
        {
            MainMenu:
            Console.WriteLine("Filmbook Data Loading Utility v1.0");
            Console.WriteLine("1. Load photos from folder");
            Console.WriteLine("2. Load photos from URL");
            Console.WriteLine("3. Purge all photos from database");
            Console.WriteLine("-----------------------------------------");
            Console.Write(">> ");

            int choice = int.Parse(Console.ReadLine());
            switch(choice)
            {
                case 1:
                    Console.WriteLine();
                    Console.WriteLine("Enter the source folder path: ");
                    string path = Console.ReadLine();
                    processFolder(path);
                    break;
                case 2:
                    Console.WriteLine("This function has not yet been implemented.");
                    goto MainMenu;
                case 3:
                    Console.WriteLine("This function has not yet been implemented.");
                    goto MainMenu;
                default:
                    Console.WriteLine("Invalid Selection");
                    goto MainMenu;
            }
        }

        private static void processFolder(string path)
        {
            DirectoryInfo di = new DirectoryInfo(path);
            List<FileInfo> files = new List<FileInfo>();
            files.AddRange(di.GetFiles("*.jpg", SearchOption.AllDirectories));
            files.AddRange(di.GetFiles("*.jpeg", SearchOption.AllDirectories));
            files.AddRange(di.GetFiles("*.gif", SearchOption.AllDirectories));
            files.AddRange(di.GetFiles("*.png", SearchOption.AllDirectories));

            Console.Write("Found {0} image files in {1}, continue processing? ", files.Count(), path);

            string choice = Console.ReadLine();
            if (choice.Contains("n"))
                return;

            FilmBookEntities ctx = new FilmBookEntities();
            User owner = ctx.Users.First();
            int count = 0;

            foreach (FileInfo imgFile in files)
            {
                Image img = Image.FromFile(imgFile.FullName);

                Photo pic = new Photo()
                {
                    Title = "Imported from " + imgFile.Name,
                    Width = img.Width,
                    Height = img.Height,
                    HorizontalResolution = img.HorizontalResolution,
                    VerticalResolution = img.VerticalResolution,
                    BitDepth = 24,
                    FileType = GetImageType(imgFile.Extension)
                };

                int thumbWidth = 95;
                int thumbHeight = 64;

                Image thumb = img.GetThumbnailImage(thumbWidth, thumbHeight, 
                    new Image.GetThumbnailImageAbort(ThumbnailCallback), 
                    IntPtr.Zero);

                using (MemoryStream ms = new MemoryStream())
                {
                    thumb.Save(ms, ImageFormat.Jpeg);
                    pic.Thumbnail = ms.ToArray();
                }

                using (MemoryStream ms = new MemoryStream())
                {
                    img.Save(ms, ImageFormat.Jpeg);
                    pic.ImageData = ms.ToArray();
                }

                pic.Owner = owner;
                ctx.Photos.AddObject(pic);

                if (count % 100 == 0)
                    ctx.SaveChanges();

                count += 1;
            }

            ctx.SaveChanges();
        }

        private static byte GetImageType(string extension)
        {
            return (byte)Enum.Parse(typeof(ImageType), extension.Replace(".", String.Empty));
        }

        private static bool ThumbnailCallback()
        {
            return false;
        }
    }
}
